<?php
class Db_connect
{
    var $user;
    var $pass;
    var $db;
    var $host;
    var $table;

    function Db_connect($host, $user_name, $password, $database)
    {
        $link = mysql_connect($host, $user_name, $password) or die ('cannot connect to the database because: ' . mysql_error());
        mysql_select_db($database);
    }

    function check_admin($usr, $pass, $table)
    {
        $sql = "select *from " . $table;
        $result = mysql_query($sql) or die("login error" . mysql_error());
        $row = mysql_fetch_object($result);
        if ($usr == $row->admin_username) {
            $f = 1;
            $_SESSION['$usr'];

        } else {
            $f = 0;
        }
        return $f;
    }

    //end of function
    function select_query($table, $fields, $condition = "", $limit = "", $display_query = "")
    {
        $this->table = $table;
        //$condition = addslashes($condition);


        $sql = "select " . $fields . " from " . $table . " " . $condition . " " . $limit;
        //echo "<br>".mysql_real_escape_string($sql);
        if ($display_query != "") {
            echo "<br> query is " . $sql . "<br>";
        }
        $result = mysql_query($sql) or die("There is some error in select query in table :" . $table . mysql_error());


        return $result;
    }

    function select_simple_query($arg)
    {

        $sql = "select " . $arg;

        //echo "<br> query is ".$sql."<br>";
        if ($display_query != "") {
            echo "<br> query is " . $sql . "<br>";
        }
        $result = mysql_query($sql) or die("There is some error in select query in table :" . $table . mysql_error());
        return $result;
    }

    function total_records($rs)
    {
        return mysql_num_rows($rs);
    }

    function insert_record($table, $field, $addslashflag = "0")
    {
        $q = "";
        $q1 = "";
        foreach ($field as $i => $value) {
            $q = $q . $i . ",";

            if ($addslashflag == 1)
                $value = addslashes($value);
            $q1 = $q1 . "'" . $value . "',";
            //echo $q1;
            //die();

        }
        $q = substr($q, 0, strlen($q) - 1);
        $q1 = substr($q1, 0, strlen($q1) - 1);

        $sql = "insert into " . $table . "(" . $q . ") values (" . $q1 . ")";
        //echo "Insert SQL==".$sql;
        $rs = mysql_query($sql) or die("There is some error in insert query in table " . $table . mysql_error());
        return $rs;
    }

    //end of insert
    /*function update_record($table,$field,$condition)
            {
               $q="";
               $q1="";
              foreach($field as $i => $value , $j=>$key)
              {
                 echo "<br>".$j;
                 /*$q=$q.$i.",";
                  if(is_numeric($value))
                   {
                        $q1=$q1.$value.",";
                   }
                   else
                   {
                     $q1=$q1."'".$value."',";
                   }*/
    //}
    /*$q=substr($q,0,strlen($q)-1);
             $q1=substr($q1,0,strlen($q1)-1);
              $sql="update  ".$table." set ".$q .") values (".$q1.")";
              //echo "Insert SQL==".$sql;
               $rs=mysql_query($sql) or die("There is some error in insert query:".mysql_error());
              return $rs;*/
    //}//end of insert
    function insert_as_select($table1, $table2, $field, $condition)
    {
        $q = "";
        $q1 = "";
        foreach ($field as $i => $value) {
            $q = $q . $i . ",";
            $q1 = $q1 . "'" . $value . "',";
            //echo $q1;
            //die();

        }
        $q = substr($q, 0, strlen($q) - 1);
        $q1 = substr($q1, 0, strlen($q1) - 1);

        $sql = "insert into " . $table1 . "(" . $q . ") (select " . $q . " from " . $table2 . " " . $condition . ")";
        //echo "Insert SQL==".$sql;
        $rs = mysql_query($sql) or die("There is some error in insert query:" . mysql_error());
        return $rs;
    }

    function update($table, $field, $condition, $addslashflag = "0")
    {

        $q = "";
        $q1 = "";
        $str = "";
        foreach ($field as $i => $value) {
            $q = $q . $i . ",";
            if ($addslashflag == 1)
                $value = addslashes($value);
            $q1 = $q1 . $value . ",";
            $str = $str . $i . " = '" . $value . "',";

            //$q=$q.$i.",";
            //$q1=$q1."'".$value."',";
        }
        $q = substr($q, 0, strlen($q) - 1);
        $q1 = substr($q1, 0, strlen($q1) - 1);
        $str = substr($str, 0, strlen($str) - 1);
        $sql = "update " . $table . " set " . $str . "" . $condition;
        // echo "<br>".$sql;
        $result_update = mysql_query($sql)or die("update query==" . mysql_error());
        return $result_update;
    }

    //end of update

    function search_record($table, $field, $criteria, $condition = "")
    {
        echo "test";
        $q = "";
        $q1 = "";
        $str = "";
        if ($criteria == "OR") {
            foreach ($field as $i => $value) {
                $q = $q . $i . ",";
                if (is_numeric($value)) {
                    if ($value != NULL) {
                        $q1 = $q1 . $value . ",";
                        $str = $str . $i . "= " . $value . " OR ";
                    } else {
                        $q1 = $q1 . " NULL " . ",";
                        $str = $str . $i . "= NULL " . " OR ";
                    }
                } else {
                    if ($value != "") {
                        $q1 = $q1 . "'" . $value . "',";
                        $str = $str . $i . "= '" . $value . "'" . " OR ";
                    } else {
                        $q1 = $q1 . "''";
                        $str = $str . $i . "= '' " . " OR ";
                    }
                }
            }
            $str = substr($str, 0, strlen($str) - 4);
            $sql = "select * from " . $table . " where  " . $str;
            $rs = mysql_query($sql);
            return $rs;
        } //end of if criteria
        else {
            foreach ($field as $i => $value) {
                $q = $q . $i . ",";
                if (is_numeric($value)) {
                    if ($value != NULL) {
                        $q1 = $q1 . $value . ",";
                        $str = $str . $i . "= " . $value . " AND ";
                    } else {
                        $q1 = $q1 . " NULL " . ",";
                    }
                } else {
                    if ($value != "") {
                        $q1 = $q1 . "'" . $value . "',";
                        $str = $str . $i . "= '" . $value . "'" . " AND ";
                    } else {
                        $q1 = $q1 . "''";
                    }
                }
            }
            $str = substr($str, 0, strlen($str) - 4);
            $sql = "select * from " . $table . " where  " . $str;
            $rs = mysql_query($sql);
            return $rs;
        }
    }

    //end of search

    function delete($table, $condition)
    {
        $sql = "delete from " . $table . " " . $condition;
        //echo $sql;
        $rs = mysql_query($sql) or die("There is some error in delete query :" . mysql_error());
        return $rs;
    }

}

//end of class DB_connect
?>